package com.example.demo.mapper;

import com.example.demo.entity.Semester;
import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Optional;

@Mapper
public interface SemesterMapper {

    // 列名需与数据库表字段一致，且与实体类属性对应
    String COLUMNS = "id, name, start_date, end_date, status, create_time";

    @Select("SELECT " + COLUMNS + " FROM semester ORDER BY start_date DESC")
    List<Semester> findAll();

    // 参数类型为 Integer（与实体类 id 一致）
    @Select("SELECT " + COLUMNS + " FROM semester WHERE id = #{semesterId}")
    Optional<Semester> findById(@Param("semesterId") Integer semesterId);

    @Insert("INSERT INTO semester (name, start_date, end_date, status, create_time) " +
            "VALUES (#{name}, #{startDate}, #{endDate}, #{status}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id") // 自增主键映射
    int insert(Semester semester);

    @Update("UPDATE semester SET name = #{name}, start_date = #{startDate}, " +
            "end_date = #{endDate}, status = #{status} WHERE id = #{id}")
    int update(Semester semester);

    @Delete("DELETE FROM semester WHERE id = #{semesterId}")
    int deleteById(@Param("semesterId") Integer semesterId);
}